using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Xml;
using System.Collections.Specialized;
using System.Collections;

namespace BlogControl
{
    class FilesManager
    {
        internal static string[] LoadFilesNames(Blog control, string directory)
        {
            string[] d = Directory.GetDirectories(HttpContext.Current.Server.MapPath(directory));
            ArrayList al = new ArrayList();
            for (int i = 0; i < d.Length; i++)
            {
                
                if (control.DrawExternalCalendar)
                {
                    control.PostsDates.Add(Path.GetFileName(d[i]));//it's actually a directory name
                }
                if (!string.IsNullOrEmpty(control.DatesFilter))
                {
                    if (Path.GetFileName(d[i]) != control.DatesFilter) continue;
                }
                string[] files = Directory.GetFiles(d[i]);
                al.AddRange(files);
            }
            string[] s = (string[])al.ToArray(typeof(string));
            Array.Reverse(s);
            return s;
        }

        internal static StreamReader OpenFile(string filepath)
        {
            return File.OpenText(filepath);
        }


        internal static string AddPost(DateTime dateTime, string title, string body, string username, string directory)
        {
            string d = HttpContext.Current.Server.MapPath(Path.Combine(directory, dateTime.ToString("yyyy-MM-dd")));
            if (!Directory.Exists(d))
            {
                Directory.CreateDirectory(d);
            }
            string fileName = Path.Combine(d, Guid.NewGuid().ToString() + ".xml");
            XmlTextWriter writer = new XmlTextWriter(fileName,Encoding.UTF8);
            try
            {
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 2;
                writer.WriteStartDocument();
                writer.WriteStartElement("Post");
                writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
                writer.WriteAttributeString("xsi:noNamespaceSchemaLocation", "Template.xsd");
                writer.WriteStartElement("Title");
                writer.WriteCData(title);
                writer.WriteEndElement();

                writer.WriteElementString("Date", dateTime.ToString("yyyy-MM-dd HH:mm:ss"));

                writer.WriteStartElement("Writer");
                writer.WriteCData(username);
                writer.WriteEndElement();

                writer.WriteStartElement("Body");
                writer.WriteCData(body);
                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.Flush();
            }
            finally
            {
                try
                {
                    writer.Close();
                }
                catch { }
            }
            return fileName;
        }

        internal static void AddComment(string directory,string id, string username, string subject, string body, DateTime dateTime)
        {
            string[] d = Directory.GetDirectories(HttpContext.Current.Server.MapPath(directory));
            for (int i = 0; i < d.Length; i++)
            {
                DirectoryInfo di=new DirectoryInfo(d[i]);
             
                FileInfo[] files =  di.GetFiles();
                for (int j = 0; j < files.Length; j++)
                {
                    if (files[j].Name == id + ".xml")
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load(files[j].FullName);
                        XmlNode node = doc.DocumentElement.SelectSingleNode("Comments");
                        if (node == null)
                        {
                            node = doc.CreateElement("Comments");
                            doc.DocumentElement.AppendChild(node);
                        }
                        XmlElement newComemnt = doc.CreateElement("comment");
                        XmlAttribute att1 = doc.CreateAttribute("date");
                        att1.Value = dateTime.ToString("yyyy-MM-dd HH:mm:ss");

                        newComemnt.Attributes.Append(att1);
                        att1 = doc.CreateAttribute("writer");
                        att1.Value = username;
                        newComemnt.Attributes.Append(att1);
                        XmlNode sub = doc.CreateElement("subject");
                        sub.InnerText = subject;
                        XmlNode bo = doc.CreateElement("body");
                        bo.InnerText = body;
                        newComemnt.AppendChild(sub);
                        newComemnt.AppendChild(bo);
                        node.AppendChild(newComemnt);
                        doc.Save(files[j].FullName);
                    }
                }
            }
        }
    }
}
